package com.wd.live.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import com.wd.live.R;
import com.wd.live.base.BaseActivity;
import com.wd.live.base.BaseViewModel;
import com.wd.live.bean.BDbankcardBean;
import com.wd.live.databinding.ActivityBDBinding;
import com.wd.live.model.SpUtils;
import com.wd.live.model.utils.ToastFormat;
import com.wd.live.viewmodel.BDBankViewModel;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BDActivity extends BaseActivity<BDBankViewModel, ActivityBDBinding> {

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String bDnum = intent.getStringExtra("BDnum");
        databind.BDnum.setText(bDnum);
        String s = hideId(bDnum);
        databind.bdbankcard.setText(s);
        String name = SpUtils.getString("acName", "");
        databind.BDname.setText(name);
        SimpleDateFormat formatter = new SimpleDateFormat("绑卡时间：yyyy.MM.dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str  = formatter.format(curDate);
        databind.bdtime.setText(str);
        databind.eyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(databind.TXpwd.getTransformationMethod()==null){
                    databind.TXpwd.setTransformationMethod(new PasswordTransformationMethod());
                    databind.eyes.setImageResource(R.mipmap.eyes);
                }else{
                    databind.TXpwd.setTransformationMethod(null);
                    databind.eyes.setImageResource(R.mipmap.closeeyes);
                }
            }
        });
        databind.renzheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (databind.TXpwd.getText().toString().equals("")||databind.TXpwd.getText().length()<6){
                    ToastFormat.setText("密码格式错误");
                    ToastFormat.show();
                }else{
                    ToastFormat.setText(name);
                    ToastFormat.show();
                    viewmodel.getList(name,bDnum,"农业银行").observe(BDActivity.this, new Observer<BDbankcardBean>() {
                        @Override
                        public void onChanged(BDbankcardBean bDbankcardBean) {
                            ToastFormat.setText(bDbankcardBean.getMessage());
                            ToastFormat.show();
                            if(bDbankcardBean.getStatus().equals("0000")){
                                finish();
                            }
                        }
                    });
                }
            }
        });

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_b_d;
    }

    @Override
    public void onChanged(Object o) {

    }
    public static String hideId(String id) {
        String idNum = id.replaceAll("(\\d{4})\\d{11}(\\d{4})", "$1 **** **** $2");
        return idNum;
    }
}